<?php
namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
use app\admin\model\Note as NoteModel;
use app\admin\controller\Base;

class Cate extends Base
{

    protected $beforeActionList = [
        // 'first',
        // 'second' =>  ['except'=>'hello'],
        'delsoncate'  =>  ['only'=>'del'],
    ];

    public function index()
    {
        $cate=new CateModel();
        if(request()->isPost()){
            $sorts=input('post.');
            foreach ($sorts as $k => $v) {
                $cate->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        return view();
    }

    public function add()
    {
        $cate=new CateModel();
        if(request()->isPost()){
            $data=input('post.');
            $plevel=db('cate')->where('id',$data['pid'])->field('level')->find();
            if($plevel){
                $data['level']=$plevel['level']+1;
            }else{
               $data['level']=0; 
            }
            $validate = \think\Loader::validate('cate');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
           $add=$cate->save($data);
           if($add){
                $this->success('添加主题成功！',url('index'));
           }else{
                $this->error('添加主题失败！');
           }
           return;
        }
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        return view();
    }

    public function edit()
    {
        $id=input('id');
        $cates=db('cate')->find($id);
        if(request()->isPost()){
            $data=[
                'id'=>input('id'),
                'catename'=>input('catename'),
            ];
            $validate = \think\Loader::validate('cate');
            if(!$validate->scene('edit')->check($data)){
               $this->error($validate->getError()); die;
            }
            $save=db('cate')->update($data);
            if($save !== false){
                $this->success('修改主题成功！','index');
            }else{
                $this->error('修改主题失败！');
            }
            return;
        }
        $this->assign('cates',$cates);
        return $this->fetch();
    }

    public function del()
    {
        $id=input('id');
        $pid = db('cate')->find($id)['pid'];
        if($pid != 0){
            if(db('cate')->delete(input('id'))){
                $this->success('删除主题成功！','index');
            }else{
                $this->error('删除主题失败！');
            }
        }else{
            $this->error('初始化主题不能删除！');
        }
        
    }

    public function delsoncate(){
        $cateid=input('id'); //要删除的当前主题的id
        $cate=new CateModel();
        $sonids=$cate->getchilrenid($cateid);
        $allcateid=$sonids;
        $allcateid[]=$cateid;
        foreach ($allcateid as $k=>$v) {
            $article=new NoteModel;
            $article->where(array('cateid'=>$v))->delete();
        }
        if($sonids){
        db('cate')->delete($sonids);
        }
    }

}
